#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bool.h"
#include "data.h"
#include "key.h"
#include "tuple.h"
#include "ListaAsoc.h"
#include "dict.h"

struct sdict {
	ListaAsoc list;
};

dict dict_empty (void) {
	dict dic = (dict)calloc(1,sizeof(struct sdict));
	dic->list = la_empty();
return dic;
}

void dict_add (dict dic, Word w, Def f) {
	data definicion = data_fromString(f);
	key palabra = key_fromString(w);
	la_add(dic->list, palabra, definicion);
	definicion = data_destroy(definicion);
	palabra = key_destroy(palabra);
}

bool dict_exists (dict d, Word w) {
	key palabra = key_fromString(w);
	bool r = la_exists (d->list, palabra);
	palabra = key_destroy(palabra);
return r;
}

Def dict_search (dict d, Word w) {
	if (dict_exists(d,w)){
		key palabra = key_fromString(w);
		data q = la_search(d->list,palabra);
		palabra = key_destroy(palabra);

		return data_toString(q);
	}
	else{
		return NULL;
	}
}

void dict_del (dict d, Word w) {
	key palabra = key_fromString(w);
	la_del (d->list, palabra);
	palabra = key_destroy(palabra);
}

void dict_pprint (dict d) {
	la_pprint (d->list);
}

int dict_length (dict d) {
	int p = la_length(d->list);
return p;
}

dict dict_destroy (dict d) {
	if(d != NULL){
		la_destroy(d->list);
		free(d);
		d = NULL;
	}

	return d;
}

int word_maxLen (void) {
	return (key_maxLen());
}

